<?php
/**
 * Created by PhpStorm.
 * User: Jie
 * Date: 4/14/2016
 * Time: 3:55 PM
 */

class weixinTemplate {
    //access
    public $access_token = '';
    //行业url
    public $set_industry_url = 'https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=%s';
    public $get_industry_url= 'https://api.weixin.qq.com/cgi-bin/template/get_industry?access_token=%s';
    public $get_list_url = 'https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token==%s';
    public $get_one_url = 'https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=%s';
    public $delete_one_url = 'https://api,weixin.qq.com/cgi-bin/template/del_private_template?access_token=%s';
    public $send_message_url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s';

    //初始化
    public function __construct($token) {
        //access_token
        $this->access_token = $token;
    }

    //设置行业
    public function setIndustry($data_arr) {
        //获取access_token
        $access_token = Tools_help::getToken();
        //url
        $url = sprintf($this->set_industry_url,$access_token);
        //转换
        $str_json = json_encode($data_arr);

        //提交
        return Tools_help::weixinCurlPost($url,$str_json);
    }

    //获取行业
    public function getIndustry() {
        //获取access_token
        $access_token = Tools_help::getToken();
        //url
        $url = sprintf($this->get_industry_url,$access_token);

        //提交
        return Tools_help::weixinCurlGet($url);
    }

    //获得模板列表
    public function getTempList() {
        //access_token
        $access_token = Tools_help::getToken();

        //url
        $url = sprintf($this->get_list_url,$access_token);

        //提交
        return Tools_help::weixinCurlGet($url);
    }

    //获得某个模板的id
    public function getOneTemplate($template_id_short) {
        //数据
        $data = array(
            'template_id_short' => $template_id_short,
        );
        $data = json_encode($data);
        //url
        $url = sprintf($this->get_one_url,$this->access_token);

        return Tools_help::weixinCurlPost($url,$data);
    }

    //删除某个模板
    public function deleteOneTemplate($template_id) {
        //$data = '{"template_id" = ' . ' " %s"}' ;
        //数据
        $data = array(
            'template_id' => $template_id,
        );
        $data = json_encode($data);
        //url
        $url = sprintf($this->delete_one_url,$this->access_token);

        return Tools_help::weixinCurlPost($url,$data);
    }

    //发送模板消息
    public function sendMessageTemplate($data_arr) {
        //数据转换
        $data = json_encode($data_arr);
        //url
        $url = sprintf($this->send_message_url,$this->access_token);

        return Tools_help::weixinCurlPost($url,$data);
    }

    //判断消息是否发送成功
    public function checkSendSuccessful($data_json){
        //json
        $data = json_decode($data_json);
        if($data) {
            //发送成功
            if ($data->errmsg == 'ok') {
                return true;
            } else {
                return false;
            }
        }
    }
}